    //
    //  FindPasswordViewController.m
    //  Kids
    //
    //  Created by LiTengFei on 14/12/12.
    //  Copyright (c) 2014年 90's Technology Co.Ltd. All rights reserved.
    //

#import "ChangePasswordViewController.h"
#import "ChangePwdTableViewCell.h"


#import "ChangePasswordRequestData.h"
#import "ChangePasswordResponseData.h"
#import "RequestFactory.h"
#import "UIColor+Extend.h"

#import "VerificationCodeRequestData.h"
#import "VerificationCodeResponseData.h"
#import "SVProgressHUD.h"

#import "NSString+md5.h"


@interface ChangePasswordViewController ()<UITextFieldDelegate>

@property (nonatomic,strong) UIScrollView * backView;

@property (nonatomic,strong) UILabel * passwordLabel;
@property (nonatomic,strong) UITextField * passwordField;

@property (nonatomic,strong) UILabel * nowPasswordLabel;
@property (nonatomic,strong) UITextField * nowPasswordField;

@property (nonatomic,strong) UILabel * confirmPasswordLabel;
@property (nonatomic,strong) UITextField * confirmPasswordField;

@property (nonatomic,strong) UIButton * submitButton;

@end


@implementation ChangePasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"忘记密码";
    CGRect frame = [[UIScreen mainScreen]bounds];
    self.view.backgroundColor = [UIColor colorF2F2F2];
    self.backView = [[UIScrollView alloc]initWithFrame:frame];
    self.backView.backgroundColor = [UIColor colorF2F2F2];
    self.backView.scrollEnabled = YES;
    
    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, 200, 20)];
    label.text = self.phoneNumber;
    [self.backView addSubview:label];
    
    UIView * passwordBackView = [[UIView alloc]initWithFrame:CGRectMake(0, 30, CGRectGetWidth(frame), 120)];
    passwordBackView.backgroundColor = [UIColor whiteColor];
    
    self.passwordLabel=({
        UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, 100, 30)];
        label.text = @"当前密码";
        label;
    });
    [passwordBackView addSubview:self.passwordLabel];
    self.passwordField=({
        UITextField * field = [[UITextField alloc]initWithFrame:CGRectMake(120, 5, 180, 30)];
        field.textAlignment = NSTextAlignmentRight;
        field.secureTextEntry = YES;
        field.delegate =self;
        field.keyboardType = UIKeyboardTypeASCIICapable;
        field.returnKeyType = UIReturnKeyDone;
        field;
    });
    [passwordBackView addSubview:self.passwordField];
    [passwordBackView addSubview:singleLineWithRect(CGRectMake(0, 40, CGRectGetWidth(frame), 0.5))];
    
    self.nowPasswordLabel=({
        UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(10, 45, 100, 30)];
        label.text = @"新密码";
        label;
    });
    [passwordBackView addSubview:self.nowPasswordLabel];
    self.nowPasswordField=({
        UITextField * field = [[UITextField alloc]initWithFrame:CGRectMake(120, 45, 180, 30)];
        field.textAlignment = NSTextAlignmentRight;
        field.secureTextEntry = YES;
        field.keyboardType = UIKeyboardTypeASCIICapable;
        field.returnKeyType = UIReturnKeyDone;
        field.delegate =self;
        field;
    });
    [passwordBackView addSubview:self.nowPasswordField];
    [passwordBackView addSubview:singleLineWithRect(CGRectMake(0, 40, CGRectGetWidth(frame), 0.5))];
    
    
    self.confirmPasswordLabel=({
        UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(10, 85, 100, 30)];
        label.text = @"确认密码";
        label;
    });
    [passwordBackView addSubview:self.confirmPasswordLabel];
    self.confirmPasswordField=({
        UITextField * field = [[UITextField alloc]initWithFrame:CGRectMake(120, 85, 180, 30)];
        field.textAlignment = NSTextAlignmentRight;
        field.secureTextEntry = YES;
        field.keyboardType = UIKeyboardTypeASCIICapable;
        field.returnKeyType = UIReturnKeyDone;
        field.delegate =self;
        field;
    });
    [passwordBackView addSubview:self.confirmPasswordField];
    [passwordBackView addSubview:singleLineWithRect(CGRectMake(0, 80, CGRectGetWidth(frame), 0.5))];
    
    UIView * submitBackgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 200, CGRectGetWidth(frame), 50)];
    submitBackgroundView.backgroundColor = [UIColor whiteColor];
    
    
    UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 100, 38);
    button.center = CGPointMake(CGRectGetWidth(frame) / 2, 25);
    [button setTitle:@"确认提交" forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"yellow"] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"yellow_press"] forState:UIControlStateHighlighted];
    
    [button addTarget:self action:@selector(submitClick:) forControlEvents:UIControlEventTouchUpInside];
    [submitBackgroundView addSubview:button];
    
    [self.backView addSubview:passwordBackView];
    [self.backView addSubview:submitBackgroundView];
    [self.view addSubview:self.backView];
    
    NSNotificationCenter * notification = [NSNotificationCenter defaultCenter];
    [notification addObserver:self selector:@selector(notificationShow:) name:UIKeyboardWillShowNotification object:nil];
    [notification addObserver:self selector:@selector(notificationHide:) name:UIKeyboardWillHideNotification object:nil];
    
    
}
-(void)notificationShow:(NSNotification *)notification{

}
-(void)notificationHide:(NSNotification *)notification{
    CGRect frame = [[UIScreen mainScreen]bounds];
    self.backView.frame = frame;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
- (void)submitClick:(UIButton *)button {
    
    NSString *phoneNumber = self.phoneNumber;
    NSString * passwordOld = self.passwordField.text;
    NSString * passwordNew = self.nowPasswordField.text;
    NSString * confirmPawword = self.confirmPasswordField.text;
    if (!passwordOld ||passwordOld.length == 0) {
        [SVProgressHUD showImage:nil status:@"输入当前密码"];
        return;
    }else  if (!passwordNew || passwordNew.length == 0) {
        [SVProgressHUD showImage:nil status:@"输入新密码"];
        return;
    }else if (!confirmPawword || confirmPawword.length == 0) {
        [SVProgressHUD showImage:nil status:@"确认新密码"];
        return;
    }
    NSParameterAssert(phoneNumber.length>0);
    NSParameterAssert(passwordOld.length>0);
    NSParameterAssert(passwordNew.length>0);
    
    ChangePasswordRequestData * request = [[ChangePasswordRequestData alloc]init];
    request.phone = phoneNumber;
    request.passwordOld = [passwordOld md5];
    request.passwordNew = [passwordNew md5];
    
    [RequestFactory requestWithRequestData:request forResponseData:[ChangePasswordResponseData new] complete:^(ResponseData *responseData, NSError *error) {
        ChangePasswordResponseData * data = (ChangePasswordResponseData *)responseData;
        [SVProgressHUD showImage:nil status:data.info];
        [self.navigationController popViewControllerAnimated:YES];
    }];
}
@end
